package com.rogo.rogobox.views
{
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	import com.rogo.rogobox.App;
	import com.rogo.rogobox.AppFacade;
	import com.rogo.rogobox.models.data.Info;
	import com.rogo.rogobox.models.vo.TimelineVO;
	import com.rogo.rogobox.views.components.consts.PageMainConst;
	import com.rogo.rogobox.views.components.events.PageEvent;
	import com.rogo.rogobox.views.components.pages.pageMain;
	import com.rogo.rogobox.views.components.renderers.BlogStatusRenderer;
	
	import flash.events.MouseEvent;
	
	public class PageMainMediator extends Mediator
	{
		public function PageMainMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'pageMainMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var timelineVO:TimelineVO;
		private var mentionVO:TimelineVO;
		private var updateBlogStatusArray:Array;
		private var currentUser:String;
		private var currentDisplay:String;
		private var start:int = 0;
		private var end:int = 0;
		private var currentPage:int = 1;
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():pageMain
		{
			return viewComponent as pageMain;
		}
		
		private function updateDisplay():void
		{
			if (!updateBlogStatusArray)
				return;
			
			var blogStatusRenderer:BlogStatusRenderer;
			
			updateBlogStatusArray.reverse();
			
			for each (var blogStatus:Info in updateBlogStatusArray)
			{
				if (view.contentList.numElements < App.PAGE_COUNT)
				{
					blogStatusRenderer = new BlogStatusRenderer();
					view.contentList.addElementAt(blogStatusRenderer, 0);
				}
				else
				{
					blogStatusRenderer = view.contentList.getElementAt(App.PAGE_COUNT - 1) as BlogStatusRenderer;
					view.contentList.setElementIndex(blogStatusRenderer, 0);
				}
				blogStatusRenderer.data = blogStatus;
			}
			
			var len:int = view.contentList.numElements;
			if (len> 0)
			{
				start = (view.contentList.getElementAt(0) as BlogStatusRenderer).data.timestamp;
				end = (view.contentList.getElementAt(len - 1) as BlogStatusRenderer).data.timestamp;
				timelineVO.id = (view.contentList.getElementAt(len - 1) as BlogStatusRenderer).data.id;
			}
		}
		
		private function displayUser():void
		{
			if (currentUser && 
				currentUser == App.getInstance().mainPageUser&&
				currentDisplay == App.getInstance().mainPageDisplay)
			{
				return;
			}
			
			timelineVO = new TimelineVO();
			mentionVO = new TimelineVO();
			currentUser = App.getInstance().mainPageUser;
			currentDisplay = App.getInstance().mainPageDisplay;
			timelineVO.name = currentUser;
			var user:String = App.getInstance().verifyUser;
			
			switch (currentDisplay)
			{
				case PageMainConst.DISPLAY_MAIN:
				{
					if (currentUser == user)
					{
						view.title.text = '我的微博';
					}
					else
					{
						view.title.text = currentUser + '的微博';
					}
					break;
				}
				case PageMainConst.DISPLAY_MENTION:
				{
					view.title.text = '@提到我的微博';
					break;
				}
				case PageMainConst.DISPAY_FAVOURITE:
				{
					view.title.text = '我的收藏'
					break;
				}
				default :
					break;
			}
			
			view.pageBar.page = 1;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function listNotificationInterests():Array
		{
			return [AppFacade.LOAD_USER_TIMELINE_RESULT,
				AppFacade.DISPLAY_USER_MAIN,
				AppFacade.DISPLAY_USER_MENTIONDS,
				AppFacade.LOAD_MENTION_RESULT,
				AppFacade.DISPLAY_USER_FAVORITE,
				AppFacade.LOAD_FAVORITE_RESULT];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch (notification.name)
			{
				case AppFacade.LOAD_MENTION_RESULT:
				case AppFacade.LOAD_USER_TIMELINE_RESULT:
				case AppFacade.LOAD_FAVORITE_RESULT:
				{
					view.pageBar.enabled = true;
					updateBlogStatusArray = notification.param as Array;
					updateDisplay();
					break;
				}
				case AppFacade.DISPLAY_USER_MAIN:
				case AppFacade.DISPLAY_USER_MENTIONDS:
				case AppFacade.DISPLAY_USER_FAVORITE:
					displayUser();
					break;
				default :
					break;
			}
		}
		
		override public function onRegister():void
		{
			view.pageBar.addEventListener(PageEvent.CHANGE, pageBar_changeHandler);
			view.previous.addEventListener(MouseEvent.CLICK, previous_clickHandler);
			
			displayUser();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function pageBar_changeHandler(e:PageEvent):void
		{
			view.pageBar.enabled = false;
			if (currentDisplay == PageMainConst.DISPLAY_MAIN)
			{
				if (e.page == 1)
				{
					timelineVO.pageFlag = 0;
				}
				else
				{
					timelineVO.pageFlag = e.page < currentPage ? 2 : 1;
				}
				timelineVO.pageTime = e.page < currentPage ? start : end;
				currentPage = e.page;
				sendNotification(AppFacade.LOAD_USER_TIMELINE, timelineVO);
			}
			else if (currentDisplay == PageMainConst.DISPLAY_MENTION)
			{
				if (e.page == 1)
				{
					mentionVO.pageFlag = 0;
				}
				else
				{
					mentionVO.pageFlag = e.page < currentPage ? 2 : 1;
				}
				mentionVO.pageTime = e.page < currentPage ? start : end;
				currentPage = e.page;
				sendNotification(AppFacade.LOAD_MENTION, mentionVO);
			}
			else if (currentDisplay == PageMainConst.DISPAY_FAVOURITE)
			{
				sendNotification(AppFacade.LOAD_FAVORITE, e.page);
			}
		}
		
		private function previous_clickHandler(e:MouseEvent):void
		{
			sendNotification(AppFacade.BACK_CONTENT_VIEW);
		}
		
	}
}